package com.lizi.apidoc.utils;

/**
 * StringUtil
 * @author 张健文【zhangjw@3vjia.com】
 * @since 2020/4/1 15:29
 * @modified by:
 */
public class StringUtil {

    /**
     * 字符串是否为null或""
     * @param source
     * @return
     */
    public static boolean isNullOrEmpty(String source) {
        return source == null || source.trim().isEmpty();
    }

    /**
     * 空字符串
     * @return
     */
    public static String getEmpty()
    {
        return "";
    }

    /**
     * 移除引用
     * @param str
     * @return
     */
    public static String removeQuotes(String str) {
        return !isNullOrEmpty(str) ? str.replaceAll("'", "").replaceAll("\"", "") : "";
    }

    /**
     * 去除首尾指定字符
     * @param str 字符串
     * @param element 指定字符
     * @return
     */
    public static String trim(String str, String element){
        if (str == null || str.equals("")) return str;
        boolean beginIndexFlag = true;
        boolean endIndexFlag = true;
        do{
            int beginIndex = str.indexOf(element) == 0 ? 1 : 0;
            int endIndex = str.lastIndexOf(element) + 1 == str.length() ? str.lastIndexOf(element) : str.length();
            str = str.substring(beginIndex, endIndex);
            beginIndexFlag = (str.indexOf(element) == 0);
            endIndexFlag = (str.lastIndexOf(element) + 1 == str.length());
        } while (beginIndexFlag || endIndexFlag);
        return str;
    }

    /**
     * 首字母为小写
     * @param param
     * @return
     */
    public static String firstToLowerCase(String param) {
        char[] cs = param.toCharArray();
        if (cs[0] > '@' && cs[0] < '[') {
            cs[0] = (char)(cs[0] + 32);
        }

        return String.valueOf(cs);
    }
}
